using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Windows.Forms;
using System.Xml.Schema;
using System.IO;
using System.Drawing;
using OpenComposite.Base;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.Repository
{
	[RepositoryItem(ItemType.Repository, "repositories", "repository_id")]
	public class RepositoryObject : RepositoryItemBaseEx
	{
		#region Initialization
		/// <summary>
		/// Initializes a new instance of the <see cref="Repository"/> class.
		/// </summary>
		public RepositoryObject()
			: base("repository_id", "repository_nm", "repository_desc", "repository_ntext",
			Resources.sNewRepository)
		{
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="Repository"/> class.
		/// </summary>
		/// <param name="bCreateNew">if set to <c>true</c> [b create new].</param>
		public RepositoryObject(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="Repository"/> class.
		/// </summary>
		/// <param name="id">The id.</param>
		public RepositoryObject(int id)
			: this()
		{
			this.Load(id);
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the domain ID.
		/// </summary>
		/// <value>The domain ID.</value>
		public override int DomainId
		{
			get
			{
				return -1;
			}
			set
			{
			}
		}

		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get { return "folder_closed"; }
		}
		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return "folder_open"; }
		}
		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override Image Image
		{
			get { return Resources.folder_closed; }
		}
		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override Image SelectedImage
		{
			get { return Resources.vsfolder_open; }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.Repository; }
		}
		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return Resources.sRepsitory; }
		}
		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "repositories"; }
		}
		private static List<ItemType> _LISTALLOWED = new List<ItemType>(new ItemType[] { ItemType.Undefined });
		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return _LISTALLOWED; }
		}

		private RepositoryProperties _extProps = null;
		/// <summary>
		/// Gets the extended properties.
		/// </summary>
		/// <value>The extended properties.</value>
		public RepositoryProperties ExtendedProperties
		{
			get
			{
				if ( _extProps == null ) {
					_extProps = getXmlSerializedObject<RepositoryProperties>("diagram");
					if ( _extProps == null ) _extProps = new RepositoryProperties();
				}
				return _extProps;
			}
		}

		#endregion

		#region Methods
		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override Icon GetIcon()
		{
			Bitmap bmp = new Bitmap(this.Image);
			bmp.MakeTransparent(Color.Magenta);
			return Icon.FromHandle(bmp.GetHicon());
		}
		/// <summary>
		/// Saves this instance.
		/// </summary>
		/// <returns></returns>
		public override bool Save()
		{
			if ( _extProps != null ) {
				setXmlSerializedObject("ExtendedProperties", "diagram", _extProps);
			}
			return base.Save();
		}
		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			RepositoryObject copy = new RepositoryObject(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			return copy;
		}
		#endregion

		#region Class: RepositoryProperties
		public class RepositoryProperties
		{
			/// <summary>
			/// Initializes a new instance of the <see cref="RepositoryProperties"/> class.
			/// </summary>
			public RepositoryProperties()
			{
				this.DocumentationID = -1;
			}
			/// <summary>
			/// Gets or sets the documentation ID.
			/// </summary>
			/// <value>The documentation ID.</value>
			public int DocumentationID { get; set; }
		}
		#endregion
	}
}